Vpa.Tree = function(classes) {
    
    this.root = {
        "text":"API Documentation",
        "id":"apidocs",
        "iconCls":"icon-docs",
        children : [],
        isPkg : true
    };
    this.buildTree(classes);
    v8WriteFile(vpaConfig.outputDir+"tree.js","Docs.classData ="+JSON.stringify(this.root));
};
Vpa.Tree.prototype = {
    findPkg : function(children, name){
        for(var i=0,len=children.length;i<len;i++){
            var child = children[i];
            if(child.text == name && child.isPkg)
                return child;
        }
        return null;
    },
    findClass : function(children, name){
        for(var i=0,len=children.length;i<len;i++){
            var child = children[i];
            if(child.name == name && child.isClass)
                return child;
        }
        return null;
    },
    buildTree : function(classes){
        var findPkg = this.findPkg,
            findClass = this.findClass;
        for(var className in classes){
            if(classes[className].isPrivate)
                continue;
            var pkgs = className.split("."),
                path = this.root;
            for(var j=0,pLen=pkgs.length;j<pLen-1;j++){
                var child = findPkg(path.children, pkgs[j]);
                if(!child) {
                    child = {
                        isPkg : true,
                        children : [],
                        text : pkgs[j],
                        iconCls:"icon-pkg"
                    };
                    path.children.push(child);
                }
                path = child;
            }
            
            if(!findClass(path.children,pkgs[pkgs.length-1])){
                path.children.push({
                    isClass : true,
                    "leaf" : true,
                    "text" : pkgs[pkgs.length-1],
                    "isClass" : true,
                    "clsName" : className,
                    "iconCls":"icon-class"
                });
            }
        }
        
        function sortMethod(a,b){
            if(a.text.toLowerCase() < b.text.toLowerCase())
                return -1;
            else
                return 1;
        }
        
        function sort(root){
            if(root.isPkg){
                root.children.sort(sortMethod);
                var children = root.children;
                for(var i=0,len=children.length;i<len;i++){
                    sort(children[i])
                }
            }
            
        }
        
        sort(this.root);
    }
};